package com.mecuros.auth.handler;

import com.mecuros.common.core.util.SpringContextHolder;
import com.mecuros.common.log.event.SysLogEvent;
import com.mecuros.common.log.util.SysLogUtils;
import com.mecuros.common.security.handler.AbstractAuthenticationSuccessEventHandler;
import com.mecuros.demo.api.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author yujianbo
 * @Classname CarlosAuthenticationSuccessEventHandler
 * @Description 登陆处理
 * @Date 2022/4/28 8:26 下午
 */
@Slf4j
@Component
public class CarlosAuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {

    /**
     * 处理登录成功方法
     * <p>
     * 获取到登录的authentication 对象
     * @param authentication 登录对象
     */
    @Override
    public void handle(Authentication authentication) {
        log.info("用户：{} 登录成功", authentication.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(authentication);
        SysLog logVo = SysLogUtils.getSysLog();
        logVo.setTitle("登录成功");
        // 发送异步日志事件
        Long startTime = System.currentTimeMillis();
        Long endTime = System.currentTimeMillis();
        logVo.setTime(endTime - startTime);
        logVo.setCreateBy(authentication.getName());
        logVo.setUpdateBy(authentication.getName());
        SpringContextHolder.publishEvent(new SysLogEvent(logVo));
    }
}
